const App = getApp();
const func = require('../../../utils/func.js');

Page({
    data: {
        list: [],
        isShow: true,
        isLogin: false, // 是否登录
        userInfo: {}, // 用户信息
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let _this = this;
        _this.setData({
            isLogin: App.checkIsLogin()
        });
        _this.getUserDetail(); // 获取当前用户信息
        _this.getPageData();
    },

    // 下拉刷新
    onPullDownRefresh() {
        let _this = this;
        // 获取当前订单信息
        _this.getUserDetail(); // 获取当前用户信息
        _this.getPageData();
        wx.stopPullDownRefresh();
    },

    /**
     * 加载页面数据
     */
    getPageData: function() {
        let _this = this;
        App._requestApi(_this, App.globalData.config.apiIndexUrl, {
            apiMoneyRecharge_1: `ekey=1`, // 获取会员充值活动
        }, function(res) {
            // 特别说明：中括号[1]的数字必须与api标签的参数ekey=1值对应，否则数据对不上。
            let list = res.data.apiMoneyRecharge[1].list;
            _this.setData({
                list,
            });
        });
    },

    /**
     * 获取当前用户信息
     */
    getUserDetail() {
        let _this = this;
        App._requestApi(_this, App.globalData.config.apiUsersdetailUrl, {}, function(res) {
            if (!res.data.userInfo) {
                res.data.isLogin = false;
                wx.removeStorageSync('token'); // 移除token
                wx.removeStorageSync('users_id'); // 移除users_id
            }
            _this.setData(res.data);
        });
    },
    
    // 充值订单提交
    onSubmitOrder(e) {
        let _this = this;
        let postData = {
            action: 'recharge',
            type_id: e.currentTarget.dataset.id
        };
        App._requestPost(_this, App.globalData.config.apiHandleUserMoneyActionUrl, postData,
            function(res) {
                _this.setData({
                    moneyData: res.data.orderData ? res.data.orderData : {}
                });
                if (res.data.weChatPay.return_code == 'FAIL') {
                    App.showError(res.data.weChatPay.return_msg, () => {});
                    return false;
                }
                App.balancePayment({
                    payment: res.data.weChatPay,
                    success(res1) {
                        // 调用充值支付后续处理
                        _this._rechargePayDealWith();
                    },
                    fail(res1) {
                        if (!res.msg.error) App.showError(res.msg.error);
                        _this.getUserDetail();
                        return false;
                    },
                    complete(res1) {
                        if (!res.msg.error) App.showError(res.msg.error);
                        return false;
                    }
                });
            },
            function(res) {
                App.showError(res.msg);
            },false,'支付中'
        );
    },

    // 调用充值支付后续处理
    _rechargePayDealWith: function() {
        let _this = this;
        if (_this.data.moneyData.moneyid && _this.data.moneyData.order_number) {
            App._requestPost(_this, App.globalData.config.apiHandleUserMoneyActionUrl, {
                    action: 'rechargePay',
                    moneyid: _this.data.moneyData.moneyid,
                    order_number: _this.data.moneyData.order_number,
                },
                function(res) {
                    let tmplIds = [];
                    for (let key in res.data.tmplData) {
                        tmplIds.push(res.data.tmplData[key]['template_id']);
                    }
                    if (tmplIds.length > 0) {
                        wx.requestSubscribeMessage({
                            tmplIds: tmplIds,
                            success (res1) {
                                let is_send = false;
                                for (let key in res1) {
                                    if (res.data.tmplData[key] && 13 == res.data.tmplData[key]['send_scene'] && 'accept' == res1[key]) {
                                        if (is_send === false) {
                                            is_send = true;
                                        }
                                    }
                                }
                                if (is_send === true) {
                                    App._requestApi(_this, App.globalData.config.apiSendWeixniNoticeUrl, {
                                        result_id: _this.data.moneyData.moneyid,
                                        send_scene: 13,
                                    }, function(res2) {
                                        _this.getUserDetail();
                                    });
                                } else {
                                    _this.getUserDetail();
                                }
                            }
                        })
                    } else {
                        App.showSuccess(res.msg, function() {
                            _this.getUserDetail();
                        });
                    }
                },
                function(res) {
                    App.showError(res.msg);
                }
            );
        }
    },

    // 跳转到其他界面
    jumpToView(e) {
        func.jumpToUrl(e)
    },

    toggle() {
        this.setData({
            isShow: !this.data.isShow
        })
    },
})
